#include <uci.h>
#include "bridge_service.h"
#include "data_type.h"
#include "idc_ubus.h"
#include "ep_gpio.h"

#define UCI_CONFIG_FILE "/etc/config/idc_config"
#define NUM_THREADS             6
BridgeCfg bridgeCfg;

/*********************************************
* 载入配置文件,并遍历Section.
*/
void idc_load_config()
{
    struct uci_package * pkg = NULL;
    struct uci_element *e;
    struct uci_context * ctx = NULL;
    char *tmp;
    const char *value;

    ctx = uci_alloc_context(); // 申请一个UCI上下文.
    if (UCI_OK != uci_load(ctx, UCI_CONFIG_FILE, &pkg))
        goto cleanup; //如果打开UCI文件失败,则跳到末尾 清理 UCI 上下文.


    /*遍历UCI的每一个节*/
    uci_foreach_element(&pkg->sections, e)
    {
        struct uci_section *s = uci_to_section(e);

        if(!strcmp("conf",s->type)) //this section is a meter
        {
            /**网桥数据读取配置**/
            if (NULL != (value = uci_lookup_option_string(ctx, s, "bridge1_ip")))
            {
                bridgeCfg.bridge1_ip = strdup(value);
                printf("%s's bridge1_ip is %s.\n",s->e.name,value);
            }
            if (NULL != (value = uci_lookup_option_string(ctx, s, "bridge2_ip")))
            {
                bridgeCfg.bridge2_ip = strdup(value);
                printf("%s's bridge2_ip is %s.\n",s->e.name,bridgeCfg.bridge2_ip);
            }
            if (NULL != (value = uci_lookup_option_string(ctx, s, "bridge3_ip")))
            {
                bridgeCfg.bridge3_ip = strdup(value);
                printf("%s's bridge3_ip is %s.\n",s->e.name,bridgeCfg.bridge3_ip);
            }
            if (NULL != (value = uci_lookup_option_string(ctx, s, "bridge4_ip")))
            {
                bridgeCfg.bridge4_ip = strdup(value);
                printf("%s's bridge4_ip is %s.\n",s->e.name,bridgeCfg.bridge4_ip);
            }

            if (NULL != (value = uci_lookup_option_string(ctx, s, "bridge1_sn")))
            {
                bridgeCfg.bridge1_sn = strdup(value);
                printf("%s's bridge1_sn is %s.\n",s->e.name,value);
            }
            if (NULL != (value = uci_lookup_option_string(ctx, s, "bridge2_sn")))
            {
                bridgeCfg.bridge2_sn = strdup(value);
                printf("%s's bridge2_sn is %s.\n",s->e.name,bridgeCfg.bridge2_sn);
            }
            if (NULL != (value = uci_lookup_option_string(ctx, s, "bridge3_sn")))
            {
                bridgeCfg.bridge3_sn = strdup(value);
                printf("%s's bridge3_ip is %s.\n",s->e.name,bridgeCfg.bridge3_sn);
            }
            if (NULL != (value = uci_lookup_option_string(ctx, s, "bridge4_sn")))
            {
                bridgeCfg.bridge4_sn = strdup(value);
                printf("%s's bridge4_sn is %s.\n",s->e.name,bridgeCfg.bridge4_sn);
            }
        }
        // 如果您不确定是 string类型 可以先使用 uci_lookup_option() 函数得到Option 然后再判断.
        // Option 的类型有 UCI_TYPE_STRING 和 UCI_TYPE_LIST 两种.


    }
    uci_unload(ctx, pkg); // 释放 pkg
    cleanup:
    uci_free_context(ctx);
    ctx = NULL;
}

void init(){
    // idc_load_config();
    // ep_gpio_export(448);
    // ep_gpio_direction(448,2);
}
int main(int argc, char* argv[])
{
    init();
    // demo_snmp_get();
    pthread_t th_ubus;
    #if 1
    pthread_create(&th_ubus, NULL, (void*)ubus_worker, NULL);
    while (true)
    {
        sleep(1);
    }
    #endif
    return 0;
}
